<?php

use \dibi;

require_once dirname(__FILE__) . "/../pfccontainer.class.php";

class pfcContainer_Azure extends pfcContainerInterface {

    public function pfcContainer_Azure() {
        pfcContainerInterface::pfcContainerInterface();
    }

    public function getDefaultConfig() {
        return pfcContainerInterface::getDefaultConfig();
    }

    public function init(&$c) {
        return pfcContainerInterface::init($c);
    }

    public function setMeta($group, $subgroup, $leaf, $leafvalue = NULL) {
        $c = & pfcGlobalConfig::Instance();

        $server = $c->serverid;

        if ($leafvalue == NULL) {
            $leafvalue = " ";
        };
        $leafvalue = str_replace("'", "''", $leafvalue);

        $sql_count = "SELECT TOP 1 COUNT(*) AS C FROM [phpfreechat] WHERE [server]='$server' AND [groupg]='$group' AND [subgroup]='$subgroup' AND [leaf]='$leaf'";
        $sql_insert = "INSERT INTO [phpfreechat] (server, groupg, subgroup, leaf, leafvalue, timestampg) VALUES('$server', '$group', '$subgroup', '$leaf', '$leafvalue', " . time() . ")";
        $sql_update = "UPDATE [phpfreechat] SET leafvalue='$leafvalue', timestampg=" . time() . " WHERE  server='$server' AND groupg='$group' AND subgroup='$subgroup' AND leaf='$leaf'";

        $res = dibi::query($sql_count);
        $row = $res->fetch();

        if ($row['C'] == 0) {
            dibi::query($sql_insert);
            return 0; // value created
        } else {
            dibi::query($sql_update);
            return 1; // value overwritten
        }
    }

    function getMeta($group, $subgroup = null, $leaf = null, $withleafvalue = false) {
        $c = & pfcGlobalConfig::Instance();

        $ret = array();
        $ret["timestamp"] = array();
        $ret["value"] = array();

        $server = $c->serverid;

        $sql_where = "";
        $sql_group_by = "";
        $value = "leafvalue";

        if ($group != NULL) {
            $sql_where .= " AND [groupg]='$group'";
            $value = "subgroup";
            $sql_group_by = "GROUP BY $value";
        }

        if ($subgroup != NULL) {
            $sql_where .= " AND [subgroup]='$subgroup'";
            $value = "leaf";
            $sql_group_by = "";
        }

        if ($leaf != NULL) {
            $sql_where .= " AND [leaf]='$leaf'";
            $value = "leafvalue";
            $sql_group_by = "";
        }

        # GOLEMQUERY #4 
        $sql_select = "SELECT $value, [timestampg] FROM [phpfreechat] WHERE [server]='$server' $sql_where $sql_group_by ORDER BY [timestampg]";
        $thisresult = dibi::query($sql_select);

        if ($thisresult->count()) {
            #while ($regel = mysql_fetch_array($thisresult))
            while ($regel = $thisresult->fetch()) {
                $ret["timestamp"][] = $regel["timestampg"];
                if ($value == "leafvalue") {
                    if ($withleafvalue) {
                        if ($regel[$value] == " ") {
                            $regel[$value] = false;
                        }
                        $ret["value"][] = $regel[$value];
                    }
                    else {
                        $ret["value"][] = NULL;
                    }
                }
                else
                    $ret["value"][] = $regel[$value];
            }
        }
        return $ret;
    }

    function incMeta($group, $subgroup, $leaf) {
        $c = & pfcGlobalConfig::Instance();

        $server = $c->serverid;
        $time = time();


        $sql_count = "SELECT TOP 1 COUNT(*) AS C FROM [phpfreechat] WHERE [server]='$server' AND [groupg]='$group' AND [subgroup]='$subgroup' AND [leaf]='$leaf'";
        $res = dibi::query($sql_count);
        $row = $res->fetch();
        if ($row['C'] == 0) {
            $leafvalue = 1;
            $sql_insert = "INSERT INTO [phpfreechat](server, groupg, subgroup, leaf, leafvalue, timestampg) VALUES('$server', '$group', '$subgroup', '$leaf','$leafvalue', " . $time . ")";
            $res = dibi::query($sql_insert);
        } else {

            $sql_update = "UPDATE [phpfreechat] SET [leafvalue]=([leafvalue]+1), [timestampg]=" . time() . " OUTPUT INSERTED.leafvalue WHERE  [server]='$server' AND [groupg]='$group' AND [subgroup]='$subgroup' AND [leaf]='$leaf'";
            $res = dibi::query($sql_update);
            $row = $res->fetch();
            $leafvalue = $row['leafvalue'];
        }

        $ret["value"][] = $leafvalue;
        $ret["timestamp"][] = $time;

        return $ret;
    }

    function rmMeta($group, $subgroup = null, $leaf = null) {
        $c = & pfcGlobalConfig::Instance();

        $server = $c->serverid;

        $sql_delete = "DELETE FROM [phpfreechat] WHERE [server]='$server'";

        if ($group != NULL) {
            $sql_delete .= " AND [groupg]='$group'";
        }
        if ($subgroup != NULL) {
            $sql_delete .= " AND [subgroup]='$subgroup'";
        }
        if ($leaf != NULL) {
            $sql_delete .= " AND [leaf]='$leaf'";
        }

        dibi::query($sql_delete);
        return true;
    }

    function encode($str) {
        return $str;
        //return addslashes(urlencode($str));
    }

    function decode($str) {
        return $str;
        //return urldecode(stripslashes($str));
    }

}